import type {
  OthersFrameworkConfig,
  PlatformElementConfig,
  StyleConfigMap,
} from '../../interfaces'
import { preprocessElementConfigs } from '../personalization'
import { COMMON_PAGE_ELEMENTS } from './common'

/**
 * 页面元素配置
 */
export const GREASY_FORK_PAGE_ELEMENTS: Record<string, PlatformElementConfig> = {
  ...COMMON_PAGE_ELEMENTS,
  // 基础容器
  CONTENT_CONTAINER: {
    id: 'content-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `.sidebarred-main-content`,
    inherit: false,
    styleVar: '--page-content-container',
  },
  HEADER_CONTAINER: {
    id: 'header-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#main-header`,
    inherit: false,
    styleVar: '--page-header-container',
  },
  RESULT_CONTAINER: {
    id: 'result-container',
    category: 'content-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#browse-script-list`,
    inherit: false,
    styleVar: '--page-result-container',
  },
  RESULT_WRAPPER: {
    id: 'result-wrapper',
    category: 'content-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#browse-script-list li`,
    inherit: false,
    styleVar: '--page-result-wrapper',
  },
  ARTICLE_CONTAINER: {
    id: 'article-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 55,
    selector: `article`,
    inherit: false,
    styleVar: '--page-article-container',
  },
  TITLE_CONTAINER: {
    id: 'title-container',
    category: 'article-container',
    categoryKey: 'category',
    priority: 55,
    selector: `#browse-script-list li article h2`,
    inherit: false,
    styleVar: '--page-article-container',
  },
  META_CONTAINER: {
    id: 'meta-container',
    category: 'article-container',
    categoryKey: 'category',
    priority: 55,
    selector: `article .script-meta-block`,
    inherit: false,
    styleVar: '--page-meta-container',
  },
  ARTICLE_CONTENT: {
    id: 'article-content',
    category: 'article-container',
    categoryKey: 'category',
    priority: 60,
    selector: 'article :is(span, p)',
    inherit: false,
    styleVar: '--page-article-content',
  },
  ARTICLE_AUTHOR: {
    id: 'article-author',
    category: 'article-container',
    categoryKey: 'category',
    priority: 60,
    selector: '.script-list-author a',
    inherit: false,
    styleVar: '--page-article-author',
  },
  PAGINATION_CONTAINER: {
    id: 'pagination-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `.pagination`,
    inherit: false,
    styleVar: '--page-pagination-container',
  },
  COMMON_CONTAINER: {
    id: 'common-container',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-info`,
    inherit: false,
    styleVar: '--page-common-container',
  },
  CONTENT_PRE: {
    id: 'content-pre',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-content pre`,
    inherit: false,
    styleVar: '--page-content-pre',
  },
  CONTENT_CODE: {
    id: 'content-code',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-content code, .script-list-description code, .text-content code`,
    inherit: false,
    styleVar: '--page-content-code',
  },
  CONTENT_TABLE: {
    id: 'content-table',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-content table, .width-constraint table`,
    inherit: false,
    styleVar: '--page-content-table',
  },
  CONTENT_HR: {
    id: 'content-hr',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-content hr, .text-content hr`,
    inherit: false,
    styleVar: '--page-content-hr',
  },
  CONTENT_BLOCK_QUOTE: {
    id: 'content-blockquote',
    category: 'main-container',
    categoryKey: 'category',
    priority: 10,
    selector: `#script-content blockquote`,
    inherit: false,
    styleVar: '--page-content-blockquote',
  },
}
// 预处理配置
const PROCESSED_GREASY_FORK_ELEMENTS = preprocessElementConfigs(GREASY_FORK_PAGE_ELEMENTS)

export type GreasyForkElements = typeof GREASY_FORK_PAGE_ELEMENTS
export type GreasyForkElementKey = keyof typeof GREASY_FORK_PAGE_ELEMENTS
export type GreasyForkElementValue = typeof GREASY_FORK_PAGE_ELEMENTS[GreasyForkElementKey]

/**
 * 框架配置类型
 */
export type GreasyForkFrameworkConfig = {
  [K in keyof OthersFrameworkConfig]: Partial<StyleConfigMap>
}

/**
 * 框架配置
 */
export const GREASY_FORK_FRAMEWORK_CONFIG: GreasyForkFrameworkConfig = {
  GLOBAL: {
    shortcuts: [
      PROCESSED_GREASY_FORK_ELEMENTS.SHORTCUTS,
    ],
    pageTheme: [
      PROCESSED_GREASY_FORK_ELEMENTS.PAGE_THEME,
    ],
    loadingTheme: [
      PROCESSED_GREASY_FORK_ELEMENTS.PAGE_LOADING,
    ],
    loadingPhrases: [
      PROCESSED_GREASY_FORK_ELEMENTS.PAGE_LOADING,
    ],
    elementHide: [
      PROCESSED_GREASY_FORK_ELEMENTS.PAGE_ELEMENTS,
    ],
    pageNum: [
      PROCESSED_GREASY_FORK_ELEMENTS.CONTENT_CONTAINER,
    ],
  },
  WRAPPER: {
  },
  TITLE: {
  },
  META: {
  },
  CONTENT: {
  },
}
